<?php

/**
 * @author minhhtn
 * @copyright 2009
 */

   # first get a mysql connection as per the FAQ
set_time_limit(0);
include_once (PATH_SQL . 'category_sql.php');
$sPath = PATH_CONTROL_ADMIN;
//$d = dir($sPath);

function check_name($name, $db_category)
{
    $sSql = "SELECT * FROM ams_product_category WHERE product_category_name like '{$name}' AND product_category_parent_id=0 ";
    return $db_category->getRecordsetByPKExtend($sSql);
}

function get_child($category_id, $name,$db_category)
{
    $sSql = "SELECT * FROM ams_product_category WHERE product_category_parent_id={$category_id} AND product_category_name like '{$name}'";
    return $db_category->getRecordsetByPKExtend($sSql);
}

$db_category = new Db_Categoiry();
$sort = 0;

require_once PATH_CONTROL_ADMIN.'Excel/reader.php';

$reader = new Spreadsheet_Excel_Reader();
$reader->setOutputEncoding("UTF-8");

$reader->read(PATH_CONTROL_ADMIN."categoryTN.xls");
//$reader->read(PATH_CONTROL_ADMIN."Book1.xls");

for ($i = 1; $i <= $reader->sheets[0]["numRows"]; $i++)
{
	for ($j = 1; $j <= $reader->sheets[0]["numCols"]; $j++)
	{
		//print "\"".$reader->sheets[0]["cells"][$i][$j]."\",";
	}
	$arr[]=$reader->sheets[0]["cells"][$i];
	//echo "\n";
}

$num_row=0;
foreach ($arr as $data)
{

	$aData=null;
        $pre_id=0;
        $arr = explode('>', $data[2]);
        $count = count($arr);
        print_r($count);
        print_r($arr);
       
        for ($i = 0; $i < $count; $i++)
        {        	
            $now_name = mb_convert_encoding($arr[$i],"UTF-8","auto");
            //$now_name=$arr[$i];
          
            //THE FIRST ONLY NEED CHECK EXITS OR NOT
            if ($i == 0)
            {
            	$rs_check_name=check_name($now_name,$db_category);
                if (!$rs_check_name)
                {
                    $aData['product_category_name'] = $now_name;
                    $aData['product_category_parent_id'] = 0;
                    $aData['product_category_status']=1;
                    $pre_id = $db_category->doInsert($aData, false);
                }
                else
                {
                	$pre_id=$rs_check_name['product_category_id'];
                }
            }
            else
            {
                if ($i < $count - 1)
                {
                	
                    //GET ALL CHILD OF THE PRE CAT
                    $rs_child = get_child($pre_id,$now_name,$db_category);
                    if (!is_array($rs_child))
                    {
                        $aData['product_category_name'] = $now_name;
                        $aData['product_category_parent_id'] = $pre_id;
                        $aData['product_category_status']=1;
                        $pre_id = $db_category->doInsert($aData, false);
                    }
                    else
                    {
                    	$pre_id=$rs_child['product_category_id'];
                    }
                }
                //IF THE LAST ITEM INSERT WITH YAHOO ID
                else
                {
		    $aData['product_category_yahoo_id'] = $data[1];
                    $aData['product_category_name'] = $now_name;
                    $aData['product_category_parent_id'] = $pre_id;
                    $aData['product_category_status']=1;
                    $pre_id = $db_category->doInsert($aData, false);
                }
		     }
        }
	$num_row++;
	//echo $num_row;
	//echo"<br/>";
} 

?>


